import { message } from 'antd';
export default {
  namespace: 'cbjt',
  state: {
    knifeCutParams: [], // 切割常用的参数设置
    xDpos: '坐标反馈未打开', // x轴坐标
    yDpos: '坐标反馈未打开', // y轴坐标
    zDpos: '坐标反馈未打开', // z轴坐标
    wDpos: '坐标反馈未打开', // w轴坐标
    btnOpenGetPosCliked: false, // 坐标反馈按钮是否被按下
  },
  effects: {
    *handleCbjToolMsg({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val === 'updateKnifeCutParams') {
        //更新界面上显示的参数列表
        yield put({
          type: 'getKnifeCutParams',
        });
      }
      if (msg.val == 'updateCbjPosState') {
        yield put({
          type: 'updateCbjPos',
          payload: msg.cbjPosList,
        });
      }
    },

    *getKnifeCutParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'cbjTool',
        'web_request_get_knife_cut_params',
        []
      );
      yield put({
        type: 'updateKnifeCutParams',
        payload: response,
      });
    },

    *saveKnifeCutParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'cbjTool',
        'web_request_save_knife_cut_params',
        [payload]
      );
      if (response === 'ok') {
        message.success('保存成功!');
      } else {
        message.error('保存失败: ', response);
      }
      yield put({
        type: 'getKnifeCutParams',
      });
    },

    *clickOpenGetPos({ payload }, { call, put }) {
      yield put({
        type: 'updateBtnGetPosClicked',
        payload: true,
      });
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_open_get_pos', []);
      if (response === 'ok') {
        message.success('坐标反馈打开成功');
      } else {
        message.error('坐标反馈打开失败,' + response);
        yield put({
          type: 'updateBtnGetPosClicked',
          payload: false,
        });
      }
    },
    *clickCloseGetPos({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_close_get_pos', []);
      if (response === 'ok') {
        message.success('坐标反馈关闭成功');
        yield put({
          type: 'updateBtnGetPosClicked',
          payload: false,
        });
        yield put({
          type: 'updateCbjPos',
          payload: ['坐标反馈未打开', '坐标反馈未打开', '坐标反馈未打开', '坐标反馈未打开'],
        });
      } else {
        message.error('坐标反馈关闭失败,' + response);
      }
    },
    *clickRsStop({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_cbj_rs_stop', []);
      if (response === 'ok') {
        message.success('手动急停成功');
      } else {
        message.error('手动急停失败,' + response);
      }
    },

    *clickCbjReset({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_cbj_reset', []);
      if (response === 'ok') {
        message.success('手动复位成功');
      } else {
        message.error('手动复位失败,' + response);
      }
    },
    *savePointCoord({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_save_point_coord', [payload]);
      if (response != 'ok') {
        message.error('数据库异常,保存失败!', 3);
      } else {
        message.success('保存成功!', 3);
      }
    },

    *axisAbsMove({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_axis_abs_move', [payload]);
    },

    *axisRelMove({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_axis_rel_move', [payload]);
    },

    *cutOneLine({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_cut_one_line', [payload]);
    },
    *cutRect({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_cut_rect', [payload]);
    },

    *cutOneLineWithCapfile({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_cut_one_line_with_capfile', [payload]);
    },

    *saveKnifeOffset({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_save_knife_offset', [payload]);
      if (response === "ok") {
        message.success("保存成功", 3);
      } else {
        message.error("保存失败" + response, 5);
      }
    },

    *openOutput({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_open_output', [payload]);
      if (response === "ok") {
        message.success("打开成功", 3);
      } else {
        message.error("打开失败" + response, 5);
      }
    },

    *closeOutput({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_close_output', [payload]);
      if (response === "ok") {
        message.success("关闭成功!", 3);
      } else {
        message.error("关闭失败," + response, 5);
      }
    },

    *cutCrossTest({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_cut_cross_test', [payload]);
      if (response === "ok") {
        message.success("切十字指令发送成功!", 3);
      } else {
        message.error("切十字指令发送失败," + response, 5);
      }
    },

    *dragTest({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_drag_test', [payload]);
      if (response === "ok") {
        message.success("拖拽指令发送成功!", 3);
      } else {
        message.error("拖拽指令发送失败," + response, 5);
      }
    },

    *downKnife({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_down_knife', []);
      if (response === "ok") {
        message.success("下刀指令发送成功!", 3);
      } else {
        message.error("下刀指令发送失败," + response, 5);
      }
    },

    *upKnife({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_up_knife', []);
      if (response === "ok") {
        message.success("抬刀指令发送成功!", 3);
      } else {
        message.error("抬刀指令发送失败," + response, 5);
      }
    },

    *fourAxisAbsMove({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjTool', 'web_request_four_axis_abs_move', [payload]);
      if (response === "ok") {
        message.success("四轴绝对移动指令发送成功!", 3);
      } else {
        message.error("四轴绝对移动指令发送失败," + response, 5);
      }
    },
    *testPlatform({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjZmc', 'web_request_test_plat_form', []);
      if (response === "ok") {
        message.success("测高指令下发成功!", 3);
      } else {
        message.error("测高指令下发失败," + response, 5);
      }
    },


    *saveCapfile({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjZmc', 'web_request_save_capfile_data', [payload]);
      if (response === "ok") {
        message.success("保存成功!", 3);
      } else {
        message.error("保存失败," + response, 5);
      }
    },

  },
  reducers: {
    updateKnifeCutParams(state, action) {
      return {
        ...state,
        knifeCutParams: action.payload,
      };
    },

    updateCbjPos(state, action) {
      return {
        ...state,
        xDpos: action.payload[0],
        yDpos: action.payload[1],
        zDpos: action.payload[2],
        wDpos: action.payload[3],
      };
    },
    updateBtnGetPosClicked(state, action) {
      return {
        ...state,
        btnOpenGetPosCliked: action.payload,
      };
    },
  },
};
